package com.zhonghan.recharge.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import com.zhonghan.recharge.service.ExportService;

@Service
public class ExportServiceImpl implements ExportService{

	@Override
	public void exportCards(HttpServletRequest request, HttpServletResponse response, String name, String cards,
			Long cardNum, Integer facePrice, Long backMonth) throws Exception {
		
		String[] cardInterval = cards.split(";");
		
		List<String> list = new ArrayList<>();
		
		for (String card : cardInterval) {
			String[] cardList = card.split("-");
			
			String cardBegin = cardList[0];
			
			String cardEnd = cardList[1];
			
			long cardb = Long.parseLong(cardBegin);
			long carde = Long.parseLong(cardEnd);
			
			for (long i = cardb; i <= carde; i++) {
				list.add(i+"");
			}
			
		}
		exportCardList(request,response,name,cardNum,facePrice,backMonth,list);
	}

	/**
	 * 导出卡段
	 * @param request
	 * @param response
	 * @param name
	 * @param cardNum
	 * @param facePrice
	 * @param backMonth
	 * @param list
	 */
	private void exportCardList(HttpServletRequest request, HttpServletResponse response, String name, Long cardNum,
			Integer facePrice, Long backMonth, List<String> list) {
		
		String fileName =  name + new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + facePrice + "元.xls";
		String sheetName = facePrice + "";
		long num = list.size() / backMonth; // 每期卡数
		num = num > 0 ? num : num + 1;
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);
		
		OutputStream os = null;
		
		try {
			String agent = request.getHeader("User-Agent");
			boolean isFirefox = agent != null && agent.contains("Firefox");
			if(isFirefox){
				fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
			}else{
				fileName = URLEncoder.encode(fileName,"UTF-8");
				
				os = response.getOutputStream();
				
				response.setContentType("application/x-msdownload");
				response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			}
			
			if(list != null && list.size() > 0){
				int row = 0;
				for (int rowIndex = 0; rowIndex < list.size()-1;) {
					
					Row dataRow = workbook.getSheet(sheetName).createRow(row);
					
					for (int columnIndex = 0; columnIndex < num; columnIndex++) {
						
						Cell cell = dataRow.createCell(columnIndex);
						cell.setCellValue(list.get(rowIndex));
						rowIndex++;
					}
					row++;
					
				}
				
				workbook.write(os);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}
